package com.chinacache.maint.workorder.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import com.chinacache.maint.commons.orm.hibernate.Page;
import com.chinacache.maint.commons.orm.hibernate.SimpleHibernateDao;
import com.chinacache.maint.workorder.entity.FieldAttribute;

@Transactional
public class FieldAttributeManager {
	private SimpleHibernateDao<FieldAttribute, Integer> fieldAttributeDao;

	@Required
	public void setSessionFactory(SessionFactory sessionFactory) {
		fieldAttributeDao = new SimpleHibernateDao<FieldAttribute, Integer>(
				sessionFactory, FieldAttribute.class);
	}

	@Transactional(readOnly = true)
	public FieldAttribute getFieldAttribute(Integer id) {
		return fieldAttributeDao.findById(id);
	}

	@Transactional(readOnly = true)
	public Page<FieldAttribute> getAllFieldAttribute(Page<FieldAttribute> page,
			Integer fieldId) {
		return fieldAttributeDao.findByCriteria(page, "id", true,
				new Criterion[] { Restrictions.eq("field.id", fieldId) });

	}

	@Transactional(readOnly = true)
	public List<FieldAttribute> getFieldAttributeByGroupId(Integer groupId) {
		String queryString = "select fa from FieldAttribute fa where fa.groupId="
				+ groupId;
		return fieldAttributeDao.find(queryString);
	}

	@Transactional(readOnly = true)
	public List<FieldAttribute> getFieldAttributeByCheckBoxLink(
			Integer checkBoxLink) {
		String queryString = "select fa from FieldAttribute fa where fa.checkBoxLink="
				+ checkBoxLink;
		return fieldAttributeDao.find(queryString);
	}

	public void saveFieldAttribute(FieldAttribute fieldAttribute) {
		fieldAttributeDao.save(fieldAttribute);
	}

	public void deleteFieldAttribute(Integer id) {
		FieldAttribute fieldAttribute = fieldAttributeDao.findById(id);
		fieldAttributeDao.delete(fieldAttribute);
	}

	public void updateFieldAttribute(FieldAttribute fieldAttribute) {
		fieldAttributeDao.update(fieldAttribute);
	}

	@Transactional(readOnly = true)
	public List<FieldAttribute> getAllFieldAttribute() {
		return fieldAttributeDao.findAll();
	}
}
